package br.edu.ufabc.gtrnp.helppo.business;

import static br.edu.ufabc.gtrnp.helppo.util.HelppoConstants.UPLOAD_PATH;
import static br.edu.ufabc.gtrnp.helppo.util.HelppoConstants.UPLOAD_URL;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts.upload.FormFile;

import br.edu.ufabc.gtrnp.helppo.util.HelppoProperties;

public class UploadBusiness {
	private static UploadBusiness business = new UploadBusiness();
	private String nomeArquivo;

	private UploadBusiness() {
	}

	public static UploadBusiness getInstance() {
		return business;
	}

	public void salvarArquivo(FormFile formFile) throws IOException {

		File file = new File(HelppoProperties.getInstance().getString(
				UPLOAD_PATH), formFile.getFileName());

//		if (!file.exists()) {
			FileOutputStream fileOutStream = new FileOutputStream(file);
			fileOutStream.write(formFile.getFileData());
			fileOutStream.flush();
			fileOutStream.close();
//		}
		nomeArquivo = formFile.getFileName();
	}

	public String getUrlArquivo() throws IOException {
		StringBuilder builder = new StringBuilder();
		builder.append(HelppoProperties.getInstance().getString(UPLOAD_URL));
		builder.append(nomeArquivo);
		return builder.toString();
	}
}
